<?php
/**
 * Page Template
 *
 * Loaded automatically by index.php?main_page=account.<br />
 * Displays previous orders and options to change various Customer Account settings
 *
 * @package templateSystem
 * @copyright Copyright 2003-2005 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version $Id: tpl_account_default.php 4086 2006-08-07 02:06:18Z ajeh $
 *
 */
global $db;

require('tpl_users_default_functions.php');
//Uncomment these lines if don't want to use file patch.sql
//create_customers_relationship_table($db);
//addColumnIfNotExist('customers_info_image', TABLE_CUSTOMERS, $db);
//addColumnIfNotExist('customers_public', TABLE_CUSTOMERS, $db);


/*****************************************process submitting*******************************************/
//var_dump($_POST);
if (isset($_POST["Upload"])) {
    check_and_upload_file($db);
} elseif (isset($_POST['Follow'])) {
    set_a_relationship($db);
} elseif (isset($_POST['public'])) { //set public or not
    echo $_POST['isPublic'];
    $isPublic = $_POST['isPublic'] == "Yes" ? 1 : 0;
    $s = "UPDATE " . TABLE_CUSTOMERS . " SET customers_public =" . $isPublic . " WHERE customers_nickname='" . $_GET['nick'] . "'";
//    echo $s;
    $db->Execute($s);
    if ($isPublic == 0) {
        //remove all relationship
        $s = "DELETE FROM " . TABLE_CUSTOMERS_RELATIONSHIP . " WHERE customers_to_id = '" . $_GET['nick'] . "'";
        $db->Execute($s);
    }
}

/*generate form to upload or follow*/
echo "<hr>";
if (isset($_SESSION['customer_id']))
    if (isItMe()) {
        generate_upload_file_form();
        generate_public_form($db, $_GET['nick']);
    } else {
        $check = "select * from customers where customers_nickname = '" . $_GET['nick'] . "' and customers_public=1;";
        $result = $db->Execute($check);
//        echo $check;
        if (!isset($result->fields)) {
            echo "<font color=red>This user's not existed or not public information</font>";
            exit();
        }

        $check = "select * from customers_relationship where customers_from_id = '" . getNickName($_SESSION['customer_id']) . "' and customers_to_id = '" . $_GET['nick'] . "';";
        $result = $db->Execute($check);
        if (!isset($result->fields))
            generate_follow_profile_form();
    }
echo "<hr>";
/*****************************************displaying page*******************************************/
$sql = "select * from " . TABLE_CUSTOMERS . " where customers_nickname = '" . $_GET['nick'] . "'";
$result = $db->Execute($sql);


?>
<div class="centerColumn" id="accountDefault">

    <!--    <h1 id="accountDefaultHeading" ><font color="red">-->
    <?php //echo "Profile of: [".$result->fields['customers_id']."] ".$result->fields['customers_nickname']; ?><!--</font></h1>-->
    <!--    <h3 id="accountDefaultHeading" ><font color="blue"><i>-->
    <?php //echo "(I am: [".$_SESSION['customer_id']."]) "; ?><!--<i></i></font></h3>-->


    <!--    <form method="post">-->
    <!--        Do you want to public your profile?-->
    <!--        <input type="checkbox" name="isPublic" value="yes" />-->
    <!--        <input type="submit" name="public" value="Submit" />-->
    <!--    </form>-->

    <!--display profile's image-->
    <?php
    if (!isset($result->fields))
        exit();
    if (isset($result->fields) && ($result->fields['customers_info_image'])) {
        echo zen_image($result->fields['customers_info_image'],'', 200,200) . "<br><br>";
    }

    //display list of users followed me or I'm following---------------------------

    /**
     * this function is only for testing, so it's not clear :D
     * @param $stars
     */
    function generate_list_of_friends($stars, $way)
    {
        $field = $way == 1 ? 'customers_to_id' : 'customers_from_id';
//        $color = $way==1?'blue':'red';
        echo "<ul class='list'>";
        while (!$stars->EOF) {
//            echo $stars->fields[$field];
            echo ' <li class="productListing-data"><a href="' . zen_href_link(FILENAME_PROFILE, 'nick=' . $stars->fields[$field], 'SSL') . '">' .
                get_image_from_nick($stars->fields[$field]) .
                '</a></li>';
//            echo "<li >".get_image_from_id($stars->fields[$field])."<li>";
            $stars->MoveNext();
        }
        echo "</ul>";
    }

    echo "Im following -->  ";
    generate_list_of_friends(get_array_I_following($_GET['nick']), 1);

    echo "<br>Followed me? <--  ";
    generate_list_of_friends(get_array_users_followed_me($_GET['nick']), 0);
    //-----------------------------------------------------------------------------


    /*show history*/
    if ($reviews_split->number_of_rows > 0) {
        if ((PREV_NEXT_BAR_LOCATION == '1') || (PREV_NEXT_BAR_LOCATION == '3')) {
            ?>
            <div id="reviewsDefaultListingTopNumber"
                 class="navSplitPagesResult"><?php echo $reviews_split->display_count(TEXT_DISPLAY_NUMBER_OF_REVIEWS); ?></div>

            <div id="reviewsDefaultListingTopLinks"
                 class="navSplitPagesLinks"><?php echo TEXT_RESULT_PAGE . ' ' . $reviews_split->display_links(MAX_DISPLAY_PAGE_LINKS, zen_get_all_get_params(array('page', 'info', 'main_page'))); ?></div>

        <?php
        }

        $reviews = $db->Execute($reviews_split->sql_query);
        while (!$reviews->EOF) {
            ?>
            <hr/>

            <div
                class="smallProductImage back"><?php echo '<a href="' . zen_href_link(FILENAME_PRODUCT_REVIEWS_INFO, 'products_id=' . $reviews->fields['products_id'] . '&reviews_id=' . $reviews->fields['reviews_id']) . '">' . zen_image(DIR_WS_IMAGES . $reviews->fields['products_image'], $reviews->fields['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT) . '</a>'; ?></div>

            <div class="forward">
                <div
                    class="buttonRow"><?php echo '<a href="' . zen_href_link(FILENAME_PRODUCT_REVIEWS_INFO, 'products_id=' . $reviews->fields['products_id'] . '&reviews_id=' . $reviews->fields['reviews_id']) . '">' . zen_image_button(BUTTON_IMAGE_READ_REVIEWS, BUTTON_READ_REVIEWS_ALT) . '</a>'; ?></div>
                <div
                    class="buttonRow"><?php echo '<a href="' . zen_href_link(zen_get_info_page($reviews->fields['products_id']), 'products_id=' . $reviews->fields['products_id']) . '">' . zen_image_button(BUTTON_IMAGE_GOTO_PROD_DETAILS, BUTTON_GOTO_PROD_DETAILS_ALT) . '</a>'; ?></div>
            </div>

            <h2><?php echo $reviews->fields['products_name']; ?></h2>

            <div
                class="rating"><?php echo zen_image(DIR_WS_TEMPLATE_IMAGES . 'stars_' . $reviews->fields['reviews_rating'] . '.gif', sprintf(TEXT_OF_5_STARS, $reviews->fields['reviews_rating'])), sprintf(TEXT_OF_5_STARS, $reviews->fields['reviews_rating']); ?></div>

            <div
                class="content"><?php echo zen_break_string(nl2br(zen_output_string_protected(stripslashes($reviews->fields['reviews_text']))), 60, '-<br />') . ((strlen($reviews->fields['reviews_text']) >= 100) ? '...' : ''); ?></div>

            <div
                class="bold"><?php echo sprintf(TEXT_REVIEW_DATE_ADDED, zen_date_short($reviews->fields['date_added'])); ?>
                &nbsp;<?php echo sprintf(TEXT_REVIEW_BY, zen_output_string_protected($reviews->fields['customers_name'])); ?></div>
            <br class="clearBoth"/>
            <?php
            $reviews->MoveNext();
        }
        ?>
    <?php
    } else {
        ?>
        <!--        <div id="reviewsDefaultNoReviews" class="content">--><?php //echo TEXT_NO_REVIEWS; ?><!--</div>-->
    <?php
    }
    ?>
    <?php
    if (($reviews_split->number_of_rows > 0) && ((PREV_NEXT_BAR_LOCATION == '2') || (PREV_NEXT_BAR_LOCATION == '3'))) {
        ?>
        <hr/>
        <div id="reviewsDefaultListingBottomNumber"
             class="navSplitPagesResult"><?php echo $reviews_split->display_count(TEXT_DISPLAY_NUMBER_OF_REVIEWS); ?></div>

        <div id="reviewsDefaultListingBottomLinks"
             class="navSplitPagesLinks"><?php echo TEXT_RESULT_PAGE . ' ' . $reviews_split->display_links(MAX_DISPLAY_PAGE_LINKS, zen_get_all_get_params(array('page', 'info', 'main_page'))); ?></div>
        <br class="clearBoth"/>
    <?php
    }
    //    var_dump($_SESSION);
    ?>

</div>